<?php
defined('_JEXEC') or die('Restricted access');
$app = JFactory::getApplication();
$input = $app->input;
$itemid = $input->get('Itemid');

$item  = $input->getArray();

$this->activeUsers = json_decode($item['jsonData']);

$vName = $input->get('view');

$scale = $input->get('scale','day');
?>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><i class="acquisition"></i></span>
    <div class="title-info">
        <h2 class="fontsize20">Returning Users</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Acquisition&nbsp;/&nbsp;Returning Users</p>
    </div>
</div>
<div class="ws-content pdall15">
    <div class="camtab">
        <!-- <nav>
          <ul class="pagination navmg fl">
            <li><a href="#">Copy</a></li>
            <li><a href="#">Excel</a></li>
            <li><a href="#">CSV</a></li>
            <li><a href="#">PDF</a></li>
          </ul>
        </nav> -->
        <nav>
            <ul class="pagination navmg fr" id="u385_input">
                <li><a <?php if($scale =='day' ):?>class="selected"<?php endif;?> href="/index.php?option=com_console&view=returningusers&Itemid=127&task=ReturningUsers&scale=day" >Day</a></li>
                <li><a <?php if($scale =='week' ):?>class="selected"<?php endif;?> href="/index.php?option=com_console&view=returningusers&Itemid=127&task=ReturningUsers&scale=week" >Week</a></li>
                <li><a <?php if($scale =='month' ):?>class="selected"<?php endif;?> href="/index.php?option=com_console&view=returningusers&Itemid=127&task=ReturningUsers&scale=month" >Month</a></li>
            </ul>
        </nav>
        <div class="clear"></div>
    </div>

<?php if ($this->activeUsers->data): ?>
    <?php  if($scale == 'day'):?>

    <table class="table table-bordered fontsize14">
        <thead>
        <tr>
            <th>Date</th>
            <th>News Users</th>
            <th>Day1</th>
            <th>Day2</th>
            <th>Day3</th>
            <th>Day4</th>
            <th>Day5</th>
            <th>Day6</th>
            <th>Day7</th>

        </tr>
        </thead>
        <?php foreach ($this->activeUsers->data as $key => $value): ?>
            <tbody>
            <tr class="bgf7">
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->date?></td>
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->newUsers?></td>
                <td><?php echo $value->a1d?></td>
                <td><?php echo $value->a2d?></td>
                <td><?php echo $value->a3d?></td>
                <td><?php echo $value->a4d?></td>
                <td><?php echo $value->a5d?></td>
                <td><?php echo $value->a6d?></td>
                <td><?php echo $value->a7d?></td>
            </tr>
            <tr class="bgf7">
                <td class="bg1"><?php echo round( $value->a1d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg2"><?php echo round( $value->a2d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg3"><?php echo round( $value->a3d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg4"><?php echo round( $value->a4d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg5"><?php echo round( $value->a5d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg6"><?php echo round( $value->a6d/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg3"><?php echo round( $value->a7d/$value->newUsers * 100 , 2) . "%";?></td>
            </tr>
            </tbody>
        <?php endforeach ?>
    </table>

        <?php elseif($scale == 'week') :?>
    <table class="table table-bordered fontsize14">
        <thead>
        <tr>
            <th>Date</th>
            <th>News Users</th>
            <th>Week1</th>
            <th>Week2</th>
            <th>Week3</th>
            <th>Week4</th>

        </tr>
        </thead>
        <?php foreach ($this->activeUsers->data as $key => $value): ?>
            <tbody>
            <tr class="bgf7">
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->date?></td>
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->newUsers?></td>
                <td><?php echo $value->a1w?></td>
                <td><?php echo $value->a2w?></td>
                <td><?php echo $value->a3w?></td>
                <td><?php echo $value->a4w?></td>
            </tr>
            <tr class="bgf7">
                <td class="bg1"><?php echo round( $value->a1w/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg2"><?php echo round( $value->a2w/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg3"><?php echo round( $value->a3w/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg4"><?php echo round( $value->a4w/$value->newUsers * 100 , 2) . "%";?></td>
            </tr>
            </tbody>
        <?php endforeach ?>
    </table>

        <?php elseif($scale == 'month') :?>
    <table class="table table-bordered fontsize14">
        <thead>
        <tr>
            <th>Date</th>
            <th>News Users</th>
            <th>Month1</th>
            <th>Month2</th>
            <th>Month3</th>
        </tr>
        </thead>
        <?php foreach ($this->activeUsers->data as $key => $value): ?>
            <tbody>
            <tr class="bgf7">
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->date?></td>
                <td rowspan="2" style="vertical-align:middle;"><?php echo $value->newUsers?></td>
                <td><?php echo $value->a1m?></td>
                <td><?php echo $value->a2m?></td>
                <td><?php echo $value->a3m?></td>
            </tr>
            <tr class="bgf7">
                <td class="bg1"><?php echo round( $value->a1m/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg2"><?php echo round( $value->a2m/$value->newUsers * 100 , 2) . "%";?></td>
                <td class="bg3"><?php echo round( $value->a3m/$value->newUsers * 100 , 2) . "%";?></td>
            </tr>
            </tbody>
        <?php endforeach ?>
    </table>

        <?php endif;?>
<script>
	function saveAsCSV() {
		var csv = '';
	<?php foreach ($this->activeUsers->data as $key => $value): ?>
		csv += '<?php echo $value->date->day . '/' . $value->date->month . '/' . $value->date->year ?>'
			+ ',' + <?php echo $value->newUsers ?>
			+ ',' + <?php echo $value->a1d ?>
			+ ',' + <?php echo $value->a2d ?>
			+ ',' + <?php echo $value->a3d ?>
			+ ',' + <?php echo $value->a4d ?>
			+ ',' + <?php echo $value->a5d ?>
			+ ',' + <?php echo $value->a6d ?>
			+ ',' + <?php echo $value->a7d ?>
			+ ',' + <?php echo $value->a14d ?>
			+ ',' + <?php echo $value->a30d ?>
			+ '\n';
	<?php endforeach?>
		var a = document.createElement("a");
		a.href = 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(csv);
		a.download = 'export.csv';
		a.target = '_blank';
		document.body.appendChild(a); 
		a.click();
	}
</script>
<?php else: ?>
 	<p class="ws-tips">No report data found.</p>
<?php endif; ?>
</div>